//
//  UserTicketsViewController.m
//  WZCLApp
//
//  Created by chirui on 2018/7/9.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "UserTicketsViewController.h"
#import "TicketsTableViewCell.h"

@interface UserTicketsViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    UIView * noBodyView;
}

@end

@implementation UserTicketsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    TitleView(@"优惠券");
    [self creatEndView];
    [self creatTableView];
     self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backBtnClick)];
    
    noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-100, 200, 200) andMessageStr:@"还没有相关内容~" onView:nil];
    [self getTickestDataRequest];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatEndView{
    
    
    UIView * endView =[[UIView alloc]init];
    
    if (ISPX) {
        endView.frame = CGRectMake(0, HEIGHT-64-48-34, WIDTH, 48+32);
    }else{
        endView.frame = CGRectMake(0, HEIGHT-64-48, WIDTH, 48);
    }
    endView.backgroundColor = [UIColor whiteColor];
    
    UIButton * endBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(0, 0, WIDTH, 48);
    endBtn.backgroundColor = [UIColor clearColor];
    [endBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [endBtn setTitle:@"不使用优惠券" forState:UIControlStateNormal];
    [endBtn addTarget:self action:@selector(sureBuy:) forControlEvents:UIControlEventTouchUpInside];
    
    [endView addSubview:endBtn];
    [self.view addSubview:endView];
    
}

-(void)sureBuy:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64 -48)
                                            style:UITableViewStyleGrouped];
    
    if (ISPX) {
        
        _tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64-48-34);
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:[TicketsTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[TicketsTableViewCell reuseIdentifier]];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return _dataArray.count;
    
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 105;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSDictionary * dic = _dataArray[indexPath.row];
    
    TicketsTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[TicketsTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.priceLabel.text = dic[@"price"];
    cell.detailsLabel.text = [NSString stringWithFormat:@"%@可用",dic[@"describe"]];
    cell.nameLbel.text = dic[@"type_string"];
    cell.timelabel.text = [NSString stringWithFormat:@"%@至%@",dic[@"create_time"],dic[@"expiration_time"]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dic = _dataArray[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.block(dic);
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(void)getTickestDataRequest {
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,user_tickets_api];
    
    
    NSDictionary *  dic = @{ @"mid":USER_DEFAULTGet(@"mid"),
                             @"token":USER_DEFAULTGet(@"token"),
                             @"order_price":self.priceStr,
                             @"type":self.orderType,
                             @"page":@"1",
                             @"per_page":@"1000",
                             @"status":@"no_use",
                             @"type":self.orderType,
                             @"order_price":self.priceStr
                             };
    
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==200) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                _dataArray = responseObject[@"datas"];
            }
            
            if (_dataArray.count==0) {
                
                noBodyView.hidden = NO;
                [self.view addSubview:noBodyView];
            }else {
                
                noBodyView.hidden = YES;
            }
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextNetError;
    }];
    
}


@end
